package com.wise.mall.admin.service.ums.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wise.mall.admin.dto.request.UmsAdminParam;
import com.wise.mall.admin.dto.response.UmsAdmin;
import com.wise.mall.admin.service.ums.AdminService;
import com.wise.mall.admin.service.ums.AuthService;
import com.wise.mall.common.constant.AuthConstant;
import com.wise.mall.common.domain.response.Oauth2TokenDto;
import com.wise.mall.common.domain.response.UserDto;
import com.wise.mall.common.exception.Asserts;
import com.wise.mall.common.model.Result;
import com.wise.mall.ums.entity.Member;
import com.wise.mall.ums.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private AuthService authService;

    @Override
    public UmsAdmin register(UmsAdminParam umsAdminParam) {
        LambdaQueryWrapper<Member> wrapper = Wrappers.<Member>lambdaQuery()
                .eq(Member::getUsername, umsAdminParam.getUsername())
                .last("limit 1");
        Member member = memberMapper.selectOne(wrapper);
        if (member != null) {
            Asserts.fail("用户已注册");
        }
        Member newMember = new Member();
        newMember.setUsername(umsAdminParam.getUsername());
        newMember.setPassword(BCrypt.hashpw(umsAdminParam.getPassword()));
        newMember.setStatus(1);
        newMember.setCreateTime(LocalDateTime.now());
        memberMapper.insert(newMember);

        return BeanUtil.toBean(newMember, UmsAdmin.class);
    }

    @Override
    public Result<Oauth2TokenDto> login(String username, String password) {
        Map<String, String> params = new HashMap<>();
        params.put("client_id", AuthConstant.ADMIN_CLIENT_ID);
        params.put("client_secret", "123456");
        params.put("grant_type", "password");
        params.put("username", username);
        params.put("password", password);

        return authService.postAccessToken(params);
    }

    @Override
    public UserDto loadUserByUsername(String username) {
        LambdaQueryWrapper<Member> wrapper = Wrappers.<Member>lambdaQuery()
                .eq(Member::getUsername, username)
                .last("limit 1");
        Member member = memberMapper.selectOne(wrapper);
        if (member != null) {
            UserDto userDto = new UserDto();
            BeanUtil.copyProperties(member, userDto);
            userDto.setRoles(CollUtil.toList("后台会员"));
            return userDto;
        }
        return null;
    }

    @Override
    @Cacheable(value = "ums:admin:user", key = "#userId")
    public UmsAdmin loadAdminUserById(Long userId) {
        Member member = memberMapper.selectById(userId);
        UmsAdmin umsAdmin = BeanUtil.copyProperties(member, UmsAdmin.class);
        umsAdmin.setPassword(null);

        return umsAdmin;
    }


}
